var e = require("../../../../@babel/runtime/helpers/interopRequireWildcard").default,
  t = require("../../../../@babel/runtime/helpers/interopRequireDefault").default,
  a = require("../../../../@babel/runtime/helpers/defineProperty"),
  r = require("../../../../@babel/runtime/helpers/objectSpread2"),
  i = require("../../../../@babel/runtime/helpers/slicedToArray"),
  s = require("../../../../@babel/runtime/helpers/regeneratorRuntime"),
  n = require("../../../../@babel/runtime/helpers/asyncToGenerator"),
  o = t(require("../../../../lib/dayjs/index")),
  bank = require("./bank"),
  u = e(require("./utils"));
Page({
  useStore: function (e) {
    return {
      customerServicePhone: e.global.customerServicePhone,
      userInfo: e.user.userInfo
    }
  },

  data: {
    page_title: "添加银行卡",
    filterParams: {
      add_type: "",
      sub_type: "",
      month: u.subtractMonth(),
      sid: "0",
      aid: "0"
    },
    isShowFilter: !1,
    totalIntegral: 1e3,
    isShowRecordList: !0,
    recordList: [],
    isInitLoad: !1,
    datePickerValue: u.subtractMonth(),
    filterOptions: {
      sub_type_list: [],
      add_type_list: []
    },
    minTime: u.subtractMonth(2),
    maxTime: u.subtractMonth(),
    bankId: '',
    bankAccount: "",
    bankCode: "",
    bankNo: ""
  },
  onLoad: function (options) {
    if (options.bankId) {
      this.setData({
        page_title: '编辑银行卡',
        bankId: options.bankId
      })
      this.getBankDetail()
    } else {
      this.setData({
        page_title: '添加银行卡'
      })
    }
  },
  change(e) {
    var bankinfo = bank.bankCardAttribution(e.detail.value)
    if (bankinfo != 'error') {
      this.setData({
        bankCode: bankinfo.bankName
      })
    }
  },
  unBindBank() {
    wx.showLoading({
      title: '加载中...',
    })
    $.request['DELETE/business/bankCard/unbind/' + this.data.bankId]().then(
      res => {
        res = res[0]
        if (res.code === 20002) {
          $.msg('解绑成功')
        }
      }
    ).finally(() => {
      wx.hideLoading()
    })
  },
  getBankDetail() {
    wx.showLoading({
      title: '加载中...',
    })
    $.request['GET/business/bankCard/detail/' + this.data.bankId]().then(
      res => {

      }
    ).finally(() => {
      wx.hideLoading()
    })
  },
  bankHandle() {
    if (this.data.bankId) {
      this.unBindBank()
    } else {
      this.addbank()
    }
  },
  addbank() {
    if (!this.data.bankNo) {
      wx.showToast({
        icon: "none",
        title: '请填写卡号',
      })
      return false
    }
    if (!this.data.bankCode) {
      wx.showToast({
        icon: "none",
        title: '请填写开户行',
      })
      return false
    }
    if (!this.data.bankAccount) {
      wx.showToast({
        icon: "none",
        title: '请填写姓名',
      })
      return false
    }
    wx.showLoading({
      title: '加载中...',
    })
    $.request['POST/business/bankCard/bind']({
      bankAccount: this.data.bankAccount,
      bankCode: this.data.bankCode,
      bankNo: this.data.bankNo,
    }).then(
      res => {
        res = res[0]
        if (res.code == 20002) {
          wx.showToast({
            icon: 'none',
            title: '添加成功',
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1000);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
          })
        }
      }
    ).finally(() => {
      wx.hideLoading()
    })
  },
  switchFilterPopupVisible: function () {
    this.setData({
      isShowFilter: !this.data.isShowFilter
    })
  },
  closeFilterPopup: function () {
    this.setData({
      isShowFilter: !1
    })
  },
  requestList: function () {
    var e = this;
    return n(s().mark((function t() {
      var n, l, d, c, m, h, f, p, b, v, P, D, L;
      return s().wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            return t.next = 2, $.request["GET/business/wechat/account/detail/pageList"](e.data.filterParams);
          case 2:
            if (l = t.sent, d = i(l, 1), c = d[0], m = null == c ? void 0 : c.content, wx.stopPullDownRefresh(), h = e.data.recordList.length, f = e.data.recordList[h - 1], p = (null === (n = m.list) || void 0 === n ? void 0 : n.map((function (e) {
                return r(r({}, e), {}, {
                  coin: u.numFormat(e.coin),
                  time: (0, o.default)(1e3 * Number(null == e ? void 0 : e.time)).format("YYYY-MM-DD HH:mm")
                })
              }))) || [], h || (h = 1, f = {
                list: []
              }), b = m.month.split("-"), v = i(b, 2), P = v[0], D = v[1], e.setData(a({
                isShowRecordList: e.data.isShowRecordList || !!m.list.length,
                isInitLoad: !0
              }, "recordList[".concat(h - 1, "]"), r(r({}, f), {}, {
                add: u.numFormat(m.sum.add),
                sub: u.numFormat(m.sum.sub),
                list: f.list.concat(p),
                year: P,
                month: D
              }))), !(0, o.default)(e.data.filterParams.month).isSame(e.data.minTime, "month")) {
              t.next = 15;
              break
            }
            e.setData({
              isLoaded: !0
            }), t.next = 23;
            break;
          case 15:
            if (0 != m.aid || 0 != m.sid) {
              t.next = 22;
              break
            }
            if (!(h > 2)) {
              t.next = 18;
              break
            }
            return t.abrupt("return");
          case 18:
            L = u.subtractMonth(1, e.data.filterParams.month), e.setData(a({
              "filterParams.month": L,
              "filterParams.sid": 0,
              "filterParams.aid": 0
            }, "recordList[".concat(h, "]"), {
              list: []
            })), e.requestList(), t.next = 23;
            break;
          case 22:
            e.setData({
              "filterParams.aid": m.aid,
              "filterParams.sid": m.sid
            });
          case 23:
          case "end":
            return t.stop()
        }
      }), t)
    })))()
  },
  resetData: function () {
    this.setData({
      "filterParams.sid": 0,
      "filterParams.aid": 0,
      recordList: []
    })
  },
  onChangeDate: function (e) {
    var t = e.detail.value;
    this.setData({
      datePickerValue: t,
      "filterParams.month": t,
      isLoaded: !1
    }), this.resetData(t), this.requestList()
  },
  onFilter: function (e) {
    var t = e.detail,
      a = t.add_type,
      r = t.sub_type;
    this.setData({
      "filterParams.month": this.data.datePickerValue,
      "filterParams.add_type": a,
      "filterParams.sub_type": r,
      isLoaded: !1,
      isShowRecordList: !1,
      isShowFilter: !1
    }), this.resetData(), this.requestList()
  },
  getFilterConf: function () {
    var e = this;
    return n(s().mark((function t() {
      var a, r, n;
      return s().wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            return t.next = 2, $.request["GET/v2/coin/conf"]();
          case 2:
            a = t.sent, r = i(a, 1), n = r[0], e.setData({
              filterOptions: null == n ? void 0 : n.content
            });
          case 6:
          case "end":
            return t.stop()
        }
      }), t)
    })))()
  },
  onClickDetail: function (e) {
    var t = e.detail,
      a = t.detail,
      r = t.type,
      i = t.info_id;
    1 == (null == a ? void 0 : a.is_deleted) ? $.msg("该信息已经删除") : 1 == (null == a ? void 0 : a.is_forbidden) ? $.msg("该信息被投诉过多，无法进行查看") : 1 == (null == a ? void 0 : a.is_company) ? $.router.push("/subpackage/supplier/supplier-detail/index", {
      id: i
    }) : $.router.push("/subpackage/common/details/index", {
      uuid: i,
      mode: 10 == r ? 5 : r
    })
  },
  onReachBottom: function () {
    (0, o.default)(this.data.filterParams.month).isSame(this.data.minTime, "month") ? this.setData({
      isLoaded: !0
    }) : this.requestList()
  },
  onPullDownRefresh: function () {
    var e = this.data.datePickerValue;
    e !== (0, o.default)().format("YYYY-MM") && (e = (0, o.default)(e).add(1, "month").format("YYYY-MM"));
    var t = {
      detail: {
        value: e
      }
    };
    this.onChangeDate(t)
  }
});